#! /usr/bin/bash

cmakePath=$1
toolChainFile=$2
softwareDir=$3
isCleanRebuild=$4


# 清除安装的可执行程序
# param: 要删除的targets
cleanInstallTargets(){
    echo "clean install targets in ANDROID_ARM64 ..."
    for targetSite in ${*}
    do
        targetPath=${softwareDir}/build_android/edge_midware/${targetSite}
        if [ -e ${targetPath} ]
        then
            rm ${targetPath}
            echo " ${targetPath} has been cleaned ... "
        fi
    done
}


# 清除构建目标
# param：buildDir
cleanBuildDirectory(){
    #清除构建目标
    ${cmakePath} --build $1 --target clean -j 14 --
    #清除构建文件夹
    if [ -e $1 ]
    then
        rm -rf $1
    fi
}


#构建目标
#sourceDir=$1 buildDir=$2 buildTarget=$3
cmakeBuildTarget(){
    sourceDir=$1
    buildDir=$2
    buildTarget=$3

    if [ ${isCleanRebuild} == clean ]
    then
        cleanBuildDirectory ${buildDir}
        cleanInstallTargets master_coordinate_site
    fi

    #定义宏
    ownMacro="-DANDROID_ARM64=true -DANDROID=1 -DANDROID_NATIVE_API_LEVEL=24 -DANDROID_ABI=arm64-v8a"
    #加载cmake
    ${cmakePath} ${ownMacro} -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${toolChainFile} -S ${sourceDir} -B ${buildDir}
    #切换目录
    cd ${buildDir}
    #构建程序
    ${cmakePath} --build ${buildDir} --target ${buildTarget} -- -j 14

    #清除编译目录
    if [ ${isCleanRebuild} == clean ]
    then
        rm -rf  ${softwareDir}/query_config_ble_synergy_sites/cmakeBuild
    fi
}

#构建程序
cmakeBuildTarget ${softwareDir}/query_config_ble_synergy_sites ${softwareDir}/query_config_ble_synergy_sites/cmakeBuild/ANDROID_ARM64 install



